const app = getApp()

Page({
  data: {
    prompt: '',
    default_img: {},
    index_notice: [], //公告
    block_list: [], //楼层板块
    coupon_list: [], //优惠券
    discount_list: [], //限时折扣
    current_time: 0, //当前时间
    timer_array: [], //限时折扣计时
    search_text: '', //搜索内容
    pintuan_list: [], //拼团
    new_goods_list: [], //新品
    recommend_goods_list: [], //精品
    hot_goods_list: [], //热卖
    webSiteInfo: {},
    //轮播图属性
    adv_img: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    indicator_color: '#AAA',
    indicator_active_color: '#FFF',
    swiper_height: 150,
    copy_right: {
      is_load: -1
    },
    timer_ch: [], //计时器 句柄
    listClickFlag: 0,
    is_login: 0,
    wap_floating: {},
    floating_show:0
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({ 
      url: '../logs/logs'
    })
  },

  onLoad: function(options) {
    let that = this;
    if (options.scene) {
      let scene = options.scene;

      if (scene.substring(0, 8) == 'sourceid') {
        let sourceid = scene.substring(9);
        sourceid = isNaN(parseInt(sourceid)) ? '' : parseInt(sourceid);
        app.setSourceId(sourceid);
      }
    }
    // wx.reLaunch({
    //   url: '/pages/diyview/diyview',
    // })
    app.defaultImg(that, true);
    // 版权信息
    app.copyRightLoad(that);

    app.sendRequest({
      url:'System.Shop.wapFloating',
      data:{},
      success:function(res){
        let data = res.data;
        let code = res.code;
        if (code == 0){
          let wap_floating = app.checkEmpty(data,[]);
          wap_floating.nav_icon = app.IMG(wap_floating.nav_icon);
          if (wap_floating.is_show == 1){
            that.setData({
              floating_show : 1
            })
          }
          that.setData({ 
            wap_floating: wap_floating
          })
        }
      }
    })
  },

  onReady: function() {

  },

  onShow: function() {
    let that = this;
    app.webSiteInfo(that, true);
    app.getMemberId(that, 'loadCouponList');
    app.restStatus(that, 'listClickFlag');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let webSiteInfo = app.globalData.webSiteInfo;
    let title = app.globalData.title;
    if (webSiteInfo.title != '' && webSiteInfo.title != undefined) {
      title = webSiteInfo.title;
    }
    return {
      title: title
    }
  },

  /**
   * 首页初始化
   */
  loadInfo: function() {
    let that = this;
    let timeArray = {};
    // 轮播广告位
    app.sendRequest({
      url: 'System.Shop.advDetail',
      data: {
        ap_keyword: 'APPLET_INDEX_SWIPER',
        export_type: 'data'
      },
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          data = app.checkEmpty(data, {});
          data.advs = app.checkEmpty(data.advs, []);
          for (let index in data.advs) {
            data.advs[index].adv_image = app.IMG(data.advs[index].adv_image);
            if (index == 0) {
              app.getWindowSize(that, true, data.advs[index].adv_image);
            }
          }
          that.setData({
            adv_img: data.advs,
          })
        }
      }
    });

    // 导航
    app.sendRequest({
      url: 'System.Shop.shopNavigationList',
      data: {
        type: 3,
        is_show: 1,
        order: 'sort asc',
        page_size: 0
      },
      success: function(res) {
        let code = res.code;
        let message = res.data.message;
        let data = res.data.data;

        if (code == 0 && app.checkArray(data)) {
          for (let index in data) {
            data[index].nav_icon = app.IMG(data[index].nav_icon);
          }
          that.setData({
            nav_list: data
          })
        }
      }
    });

    // 公告
    app.sendRequest({
      url: 'System.Shop.shopNoticeList',
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          that.setData({
            index_notice: data.data,
          })
        }
      }
    });

    // 限时折扣
    app.sendRequest({
      url: 'System.Goods.newestDiscount',
      success: function(res) {
        if (res.code == 0) {
          let discount_list = app.checkEmpty(res.data.goods_list, []);
          for (let index in discount_list) {
            discount_list[index].picture_info.pic_cover_big = app.IMG(discount_list[index].picture_info.pic_cover_big);
          }
          if (discount_list[0] != undefined) {
            timeArray[0] = {};
            timeArray[0].end = 0;
            timeArray[0].end_time = discount_list[0].end_time;
            that.timing(that, timeArray, 0);                                                     
          }
          that.setData({
            discount_list: discount_list
          })
        }
      }
    });

    //拼团
    let condition = {
      'npg.is_open': 1,
      'npg.is_show': 1
    };
    condition = JSON.stringify(condition);
    app.sendRequest({
      url: 'NsPintuan.Pintuan.goodsList',
      data: {
        'page_size': 5,
        'condition': condition,
        'order': 'npg.create_time desc'
      },
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          data = app.checkEmpty(data, {});
          let pintuan_list = app.checkEmpty(data.data, []);

          for (let index in pintuan_list) {
            pintuan_list[index].pic_cover_mid = app.IMG(pintuan_list[index].pic_cover_mid);
          }
          that.setData({
            pintuan_list: pintuan_list
          })
        }
      }
    });

    // 新品
    app.sendRequest({
      url: 'System.Goods.newGoodsList',
      data: {
        page_size: 4,
      },
      success: function(res) {
        if (res.code == 0) {
          let new_goods_list = res.data;
          for (let index in new_goods_list) {
            new_goods_list[index].pic_cover_mid = app.IMG(new_goods_list[index].pic_cover_mid);
          }
          that.setData({
            new_goods_list: new_goods_list
          })
        }
      }
    });

    // 精品
    app.sendRequest({
      url: 'System.Goods.recommendGoodsList',
      data: {
        page_size: 4,
      },
      success: function(res) {
        if (res.code == 0) {
          let recommend_goods_list = res.data;
          if (app.checkArray(recommend_goods_list)) {
            for (let index in recommend_goods_list) {
              recommend_goods_list[index].pic_cover_mid = app.IMG(recommend_goods_list[index].pic_cover_mid);
            }
          }
          that.setData({
            recommend_goods_list: recommend_goods_list
          })
        }
      }
    });

    // 热卖
    app.sendRequest({
      url: 'System.Goods.hotGoodsList',
      data: {
        page_size: 4,
      },
      success: function(res) {
        if (res.code == 0) {
          let hot_goods_list = res.data;
          if (app.checkArray(hot_goods_list)) {
            for (let index in hot_goods_list) {
              hot_goods_list[index].pic_cover_mid = app.IMG(hot_goods_list[index].pic_cover_mid);
            }
          }
          that.setData({
            hot_goods_list: hot_goods_list
          })
        }
      }
    });

    // 楼层板块
    app.sendRequest({
      url: 'System.Goods.goodsCategoryBlockWap',
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          for (let index in data.data) {
            data.data[index].img = app.IMG(data.data[index].img);
            for (let int in data.data[index].goods_list) {
              data.data[index].goods_list[int].pic_cover_small = app.IMG(data.data[index].goods_list[int].pic_cover_small);
            }
          }
          that.setData({
            block_list: data.data,

          })
        }
      }
    });
  },

  /**
   * 优惠券
   */
  loadCouponList: function() {
    let that = this;
    let uid = that.data.uid;
    uid = app.checkEmpty(uid, 0);
    app.sendRequest({
      url: 'System.Member.canReceiveCouponQuery',
      data: {
        uid: uid,
        shop_id: 0,
      },
      success: function(res) {
        if (res.code == 0) {
          let data = res.data;
          that.setData({
            coupon_list: data,
          })
        }
      }
    });
  },

  /**
   * 输入框绑定事件
   */
  searchInput: function(event) {
    let search_text = event.detail.value;
    this.setData({
      search_text: search_text
    })
  },

  /**
   * 计时
   */
  timing: function(that, timer_array, index) {
    let timer_ch = that.data.timer_ch;
    let current_time = that.data.current_time;
    let count_second = (timer_array[index].end_time - current_time);
    //首次加载
    if (count_second > 0) {
      count_second--;
      //时间计算
      let day = Math.floor((count_second / 3600) / 24);
      let hour = Math.floor((count_second / 3600) % 24);
      let minute = Math.floor((count_second / 60) % 60);
      let second = Math.floor(count_second % 60);
      //赋值
      timer_array[index].day = day;
      timer_array[index].hour = hour;
      timer_array[index].minute = minute;
      timer_array[index].second = second;
      timer_array[index].end = 0;

      that.setData({
        timer_array: timer_array
      })
    } else {
      timer_array[index].end = 1;

      that.setData({
        timer_array: timer_array
      })
    }
    //开始计时
    let timer = setInterval(function() {
      if (count_second > 0) {
        count_second--;
        //时间计算
        let day = Math.floor((count_second / 3600) / 24);
        let hour = Math.floor((count_second / 3600) % 24);
        let minute = Math.floor((count_second / 60) % 60);
        let second = Math.floor(count_second % 60);
        //赋值
        timer_array[index].day = day;
        timer_array[index].hour = parseInt(hour) < 10 ? 0 + '' + hour : hour;
        timer_array[index].minute = parseInt(minute) < 10 ? 0 + '' + minute : minute;;
        timer_array[index].second = parseInt(second) < 10 ? 0 + '' + second : second;;
        timer_array[index].end = 0;

        that.setData({
          timer_array: timer_array
        })
      } else {
        timer_array[index].end = 1;

        that.setData({
          timer_array: timer_array
        })

        clearInterval(timer);
      }
    }, 1000)
    let param = {};
    let key = 'timer_ch[' + timer_ch.length + ']';
    param[key] = timer;
    that.setData(param);
  },

  /**
   * 领取优惠券
   */
  toReceivePopup: function(e) {
    let that = this;
    let coupon_type_id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let flag = true;
    let status = 1;


    if (flag) {
      app.sendRequest({
        url: 'System.Goods.receiveGoodsCoupon',
        data: {
          coupon_type_id: coupon_type_id
        },
        success: function(res) {
          let code = res.code;
          let data = res.data;

          if (code == 0) {
            if (data > 0) {
              app.showBox(that, '领取成功');
            } else if (data == -2011) {
              app.showBox(that, '来迟了，已经领完了');
              status = 0;
            } else if (data == -2019) {
              app.showBox(that, '领取已达到上限');
              status = 0;
            } else {
              app.showBox(that, '未获取到优惠券信息');
              status = 0;
            }
            let d = {};
            let key = "coupon_list[" + index + "].status";
            d[key] = 0;

            that.setData(d);
          }
        }
      });
    }

    if (!flag) {
      let d = {};
      let key = "coupon_list[" + index + "].status";
      d[key] = 0;

      that.setData(d);
    }
  },

  /**
   * 关闭浮层
   */
  closeFloating:function(e){
    let that = this;
    that.setData({
      floating_show : 0
    })
  },

  /**
   * 链接跳转
   */
  linkJump: function (e) {
    let url = e.currentTarget.dataset.url;
    app.linkJumpDetection(url);
  }
})